﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class MainMenuScreen : MenuScreen
{
    private double initialTime = 0;

    private Texture2D background;

    MenuEntry newGameEntry, loadGameEntry, optionGameEntry, exitGameEntry;

    SpriteFont font, descfont;
    SpriteFont Font
    {
        get { return font; }
        set { font = value; }
    }

    bool hasSavedGame = false;

    public MainMenuScreen()
        : base()
    {
        Direction = MenuEntryDirection.VERTICAL;

        TransitionOnTime = TimeSpan.Zero;
        TransitionOffTime = TimeSpan.Zero;

        selectedEntry = 0;

        CheckSavedGame();
    }

    private void CheckSavedGame()
    {
        SavedGameManager save = new SavedGameManager();
        hasSavedGame = save.CheckSavedGame();
    }

    private void CreateMenuEntries()
    {
        newGameEntry = new MenuEntry("Novo Jogo");
        newGameEntry.Description = "Crie um novo personagem e comece a jogar do início.";
        newGameEntry.DescriptionPosition = new Vector2(520, 580);
        newGameEntry.Position = new Vector2(700, 200f);
        newGameEntry.Selected += NewGameEntrySelected;
        newGameEntry.DescriptionFont = descfont;
        newGameEntry.Font = font;
        MenuEntries.Add(newGameEntry);

        loadGameEntry = new MenuEntry("Carregar Jogo");
        loadGameEntry.Description = "Carregue um jogo já salvo e continue jogando de onde parou.";
        loadGameEntry.DescriptionPosition = new Vector2(520, 580);
        loadGameEntry.Position = new Vector2(700, 250f);
        loadGameEntry.Selected += LoadGameEntrySelected;
        loadGameEntry.DescriptionFont = descfont;
        loadGameEntry.Font = font;
        MenuEntries.Add(loadGameEntry);

        /*optionGameEntry = new MenuEntry("Opcoes");
        optionGameEntry.Description = "Entre caso queira alterar alguma das opções do jogo.";
        optionGameEntry.DescriptionPosition = new Vector2(520, 580);
        optionGameEntry.Position = new Vector2(700, 300f);
        optionGameEntry.Selected += OptionsEntrySelected;
        optionGameEntry.DescriptionFont = descfont;
        optionGameEntry.Font = font;
        optionGameEntry.IsSelectable = false;
        MenuEntries.Add(optionGameEntry);*/

        exitGameEntry = new MenuEntry("Sair");
        exitGameEntry.Description = "Sai do jogo.";
        exitGameEntry.DescriptionPosition = new Vector2(520, 580);
        exitGameEntry.Position = new Vector2(700, 350f);
        exitGameEntry.Selected += ExitEntrySelected;
        exitGameEntry.DescriptionFont = descfont;
        exitGameEntry.Font = font;
        MenuEntries.Add(exitGameEntry);
    }

    bool hasfocus = true;

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        if (initialTime == 0)
            initialTime = gameTime.TotalRealTime.TotalMilliseconds;

        //if (initialTime + 3000 > gameTime.TotalRealTime.TotalMilliseconds)
        //    Exit();

        hasfocus = !otherScreenHasFocus;

        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    }

    public override void LoadContent()
    {
        base.LoadContent();

        font = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont2");
        descfont = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont1");

        background = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/menuin");

        CreateMenuEntries();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            Exit();
            return;
        }

        base.HandleInput();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();
        spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }

    public void Exit()
    {
        ExitScreen();
    }

    public void NewGameEntrySelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new CharacterCreationScreen());
    }

    public void LoadGameEntrySelected(object sender, EventArgs e)
    {

        ScreenManager.AddScreen(new LoadGameScreen());

    }

    public void OptionsEntrySelected(object sender, EventArgs e)
    {
        
    }

    public void ExitEntrySelected(object sender, EventArgs e)
    {
        ExitScreen();
    }

}
